package org.hepeng.couchdb.demo;

import org.ektorp.CouchDbConnector;
import org.ektorp.ViewQuery;
import org.ektorp.ViewResult;
import org.ektorp.support.CouchDbRepositorySupport;
import org.ektorp.support.GenerateView;
import org.ektorp.support.View;

/**
 * author he peng
 * date 2022/4/26 1:16
 */

public class EktorpViewRepository extends CouchDbRepositorySupport<Long> {


    protected EktorpViewRepository(Class<Long> type, CouchDbConnector db) {
        super(type, db);
    }

    protected EktorpViewRepository(Class<Long> type, CouchDbConnector db, boolean createIfNotExists) {
        super(type, db, createIfNotExists);
    }

    protected EktorpViewRepository(Class<Long> type, CouchDbConnector db, String designDocName) {
        super(type, db, designDocName);
    }

    protected EktorpViewRepository(Class<Long> type, CouchDbConnector db, String designDocName, boolean createIfNotExists) {
        super(type, db, designDocName, createIfNotExists);
    }

    @View(name = "count" , file = "/count_view.json")
    public Long count() {

        ViewQuery viewQuery = createQuery("count").reduce(false).includeDocs(true);
        ViewResult viewResult = db.queryView(viewQuery);
        System.out.println(viewResult);
        return Integer.valueOf(viewResult.getTotalRows()).longValue();
    }
}
